#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include "lib/tinyxml/tinyxml.h"

// 简单的CBP文件解析器
class SimpleCBParser {
public:
    static bool ParseCBPFile(const std::string& filename, std::string& projectName, std::vector<std::string>& sourceFiles) {
        TiXmlDocument doc(filename.c_str());
        if (!doc.LoadFile()) {
            return false;
        }
        
        TiXmlElement* root = doc.FirstChildElement("CodeBlocks_project_file");
        if (!root) return false;
        
        TiXmlElement* project = root->FirstChildElement("Project");
        if (!project) return false;
        
        // 解析项目选项
        TiXmlElement* option = project->FirstChildElement("Option");
        while (option) {
            const char* title = option->Attribute("title");
            if (title) {
                projectName = title;
                break;
            }
            option = option->NextSiblingElement("Option");
        }
        
        // 解析源文件
        TiXmlElement* unit = project->FirstChildElement("Unit");
        while (unit) {
            const char* filename = unit->Attribute("filename");
            if (filename) {
                std::string fileStr(filename);
                if (fileStr.find(".cpp") != std::string::npos || 
                    fileStr.find(".c") != std::string::npos) {
                    sourceFiles.push_back(fileStr);
                }
            }
            unit = unit->NextSiblingElement("Unit");
        }
        
        return true;
    }
};

// AI模型配置
class AIModelManager {
private:
    std::string currentModel;
    std::string currentModelName;
    
public:
    void SetModel(int model_id, const std::string& model_name = "") {
        switch(model_id) {
            case 0: 
                currentModel = "DSAI";
                currentModelName = model_name.empty() ? "DS-V3.2exp0922" : model_name;
                break;
            case 1:
                currentModel = "UEAI";
                currentModelName = model_name.empty() ? "UE-CodeExpert" : model_name;
                break;
            case 2:
                currentModel = "OPAI";
                currentModelName = model_name.empty() ? "GPT-4" : model_name;
                break;
            default:
                currentModel = "UNKNOWN";
                currentModelName = "Unknown";
        }
        std::cout << "✅ AI模型设置: " << currentModel << " - " << currentModelName << std::endl;
    }
    
    std::string GetStatus() const {
        return "AI模型: " + currentModel + " (" + currentModelName + ")";
    }
    
    std::string AnalyzeProject(const std::string& cbpFile) {
        std::string projectName;
        std::vector<std::string> sourceFiles;
        
        if (SimpleCBParser::ParseCBPFile(cbpFile, projectName, sourceFiles)) {
            std::string analysis = "🔍 AI分析报告:\n";
            analysis += "   项目名称: " + projectName + "\n";
            analysis += "   源文件数量: " + std::to_string(sourceFiles.size()) + "\n";
            analysis += "   使用的模型: " + currentModelName + "\n";
            analysis += "   源文件列表:\n";
            
            for (size_t i = 0; i < sourceFiles.size() && i < 5; ++i) {
                analysis += "     - " + sourceFiles[i] + "\n";
            }
            if (sourceFiles.size() > 5) {
                analysis += "     ... 还有 " + std::to_string(sourceFiles.size() - 5) + " 个文件\n";
            }
            
            return analysis;
        } else {
            return "❌ 无法解析CBP文件: " + cbpFile;
        }
    }
};

int main(int argc, char* argv[]) {
    AIModelManager aiManager;
    
    // AI模型设置
    if (argc > 1 && std::string(argv[1]) == "--ai-set-model") {
        if (argc > 3) {
            int model_id = std::stoi(argv[2]);
            std::string model_name = argv[3];
            aiManager.SetModel(model_id, model_name);
        } else if (argc > 2) {
            int model_id = std::stoi(argv[2]);
            aiManager.SetModel(model_id);
        } else {
            std::cout << "用法: --ai-set-model <模型ID> [模型名称]" << std::endl;
            std::cout << "  0 - DeepSeek AI (DS-V3.2exp0922)" << std::endl;
            std::cout << "  1 - UE CodeExpert" << std::endl;
            std::cout << "  2 - GPT-4" << std::endl;
            std::cout << "示例:" << std::endl;
            std::cout << "  --ai-set-model 0 DS-V3.2exp0922" << std::endl;
            std::cout << "  --ai-set-model 1 UE-CodeExpert" << std::endl;
            std::cout << "  --ai-set-model 2 GPT-4" << std::endl;
        }
        return 0;
    }
    
    // AI模式 - CBP文件分析
    if (argc > 1 && std::string(argv[1]) == "--ai-mode") {
        if (argc > 2) {
            std::string cbpFile = argv[2];
            std::cout << aiManager.AnalyzeProject(cbpFile) << std::endl;
        } else {
            std::cout << "用法: --ai-mode <CBP文件路径>" << std::endl;
            std::cout << "示例: --ai-mode project.cbp" << std::endl;
        }
        return 0;
    }
    
    // AI状态
    if (argc > 1 && std::string(argv[1]) == "--ai-status") {
        std::cout << aiManager.GetStatus() << std::endl;
        return 0;
    }
    
    // 帮助信息
    if (argc > 1 && std::string(argv[1]) == "--help") {
        std::cout << "cbp2make_ai - AI增强的CBP到Makefile转换器" << std::endl;
        std::cout << std::endl;
        std::cout << "AI功能:" << std::endl;
        std::cout << "  --ai-set-model <id> [name]  设置AI模型" << std::endl;
        std::cout << "  --ai-mode <file.cbp>        AI分析CBP项目" << std::endl;
        std::cout << "  --ai-status                显示AI模型状态" << std::endl;
        std::cout << std::endl;
        std::cout << "传统功能:" << std::endl;
        std::cout << "  -in <file.cbp>             输入CBP文件" << std::endl;
        std::cout << "  -out <makefile>            输出makefile" << std::endl;
        std::cout << std::endl;
        std::cout << "示例:" << std::endl;
        std::cout << "  ./cbp2make_ai --ai-set-model 0 DS-V3.2exp0922" << std::endl;
        std::cout << "  ./cbp2make_ai --ai-mode project.cbp" << std::endl;
        std::cout << "  ./cbp2make_ai --ai-status" << std::endl;
        return 0;
    }
    
    // 默认行为
    std::cout << "cbp2make_ai - AI增强版本" << std::endl;
    std::cout << "使用 --help 查看AI功能" << std::endl;
    
    return 0;
}
